package cn.fate.core.common;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sun.istack.internal.NotNull;

import java.io.Serializable;

import static cn.fate.core.common.ReturnCode.*;

/**
 * @program: FtCore
 * @description: 返回类
 * @author: xubo
 * @create: 2021-09-13 17:37
 **/
public class ResponseBean<T> implements Serializable {
    public static final long serialVersionUID = 6046381587771240192L;
    /**
     * 返回码
     */
    private String code;

    /**
     * 消息
     */
    private String message = "";
    /**
     * 业务数据
     */
    private T data;

    private ResponseBean(String code, String message) {
        this.code = code;
        this.message = message;
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public boolean isSuccess() {
        return RSP_SUCCESS.equals(code);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 返回码中文描述
     *
     * @param resultCode 返回码
     * @return 中文名称
     */
    private static String getMsg(String resultCode) {
        if (ReturnCode.returnCodeMap.containsKey(resultCode)) {
            return ReturnCode.returnCodeMap.get(resultCode);
        } else {
            return "";
        }
    }

    public ResponseBean() {
    }

    /**
     * 成功
     *
     * @param <T>
     * @return
     */
    public static <T> ResponseBean<T> success() {
        ResponseBean<T> response = new ResponseBean<T>();
        response.setCode(RSP_SUCCESS);
        response.setMessage(getMsg(RSP_SUCCESS));
        return response;
    }

    /**
     * 成功
     *
     * @param code 从代码表里根据代码设置message
     * @param <T>
     * @return
     */
    public static <T> ResponseBean<T> successWithCode(String code) {
        ResponseBean<T> response = new ResponseBean<T>();
        response.setCode(RSP_SUCCESS);
        response.setMessage(getMsg(code));
        return response;
    }

    /**
     * 成功
     *
     * @param msg message设置的信息
     * @param <T>
     * @return
     */
    public static <T> ResponseBean<T> successWithMessage(String msg) {
        ResponseBean<T> response = new ResponseBean<T>();
        response.setCode(RSP_SUCCESS);
        response.setMessage(msg);
        return response;
    }

    /**
     * 成功
     *
     * @param result
     * @param <T>
     * @return
     */
    public static <T> ResponseBean<T> success(T result) {
        ResponseBean<T> response = new ResponseBean<T>();
        response.setCode(RSP_SUCCESS);
        response.setMessage(getMsg(RSP_SUCCESS));
        response.setData(result);
        return response;
    }

    /**
     * 返回指定的错误代码，错误信息直接从ReturnCode类的定义中填取
     *
     * @param code
     * @param <T>
     * @return
     */
    public static <T> ResponseBean<T> failWithCodeByDefaultMessage(String code) {
        ResponseBean<T> response = new ResponseBean<T>();
        response.setCode(code);
        response.setMessage(getMsg(code));
        return response;
    }

    /**
     * 指定错误代码及错误信息
     *
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseBean<T> failWithCodeAndMessage(String code, String message) {
        ResponseBean<T> response = new ResponseBean<T>();
        if (!StringUtils.isEmpty(message)) {
            response.setCode(code);
            response.setMessage(message);
        } else {
            response.setCode(SYSTEM_ERROR);
            response.setMessage(getMsg(SYSTEM_ERROR));
        }
        return response;
    }

    /**
     * 返回指定错误(系统异常)
     *
     * @param message 错误消息
     * @param <T>
     * @return
     */
    public static <T> ResponseBean<T> errorWithMessage(@NotNull String message) {
        ResponseBean<T> response = new ResponseBean<T>();
        response.setCode(SYSTEM_ERROR);
        response.setMessage(message);
        return response;
    }
    /**
     * 返回指定错误(业务错误)
     *
     * @param message 错误消息
     * @param <T>
     * @return
     */
    public static <T> ResponseBean<T> failWithMessage(@NotNull String message) {
        ResponseBean<T> response = new ResponseBean<T>();
        response.setCode(RSP_BUS_ERROR);
        response.setMessage(message);
        return response;
    }

    /**
     * 登录过期
     *
     * @param <T>
     * @return
     */
    public static <T> ResponseBean<T> failWithSessionOut() {
        ResponseBean<T> response = new ResponseBean<T>();
        response.setCode(NO_AUTH);
        response.setMessage(getMsg(NO_AUTH));
        return response;
    }

    /**
     * 无法找到的请求
     *
     * @param <T>
     * @return
     */
    public static <T> ResponseBean<T> failWithNotFound() {
        ResponseBean<T> response = new ResponseBean<T>();
        response.setCode(SYSTEM_PATH_NOEXIST);
        response.setMessage(getMsg(SYSTEM_PATH_NOEXIST));
        return response;
    }

    /**
     * 系统错误错误
     *
     * @param <T>
     * @return
     */
    public static <T> ResponseBean<T> failWithSystemError() {
        ResponseBean<T> response = new ResponseBean<T>();
        response.setCode(SYSTEM_ERROR);
        response.setMessage(getMsg(SYSTEM_ERROR));
        return response;
    }
}
